import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Modal, Tabs} from "antd";
import {create, hideCreate, query} from "./actions";
import ClientModal from "./ClientModal";
import ContractModal from "./ContractModal";
import {getCurrentOrganization, getPrincipal} from "../../lib/identity";
import {formatDate} from "../../lib/func";
import ClientFeeModal from "./ClientFeeModal";
import VehicleFeeModal from "./VehicleModal";
import LinePriceInCreateModal from "./LinePriceInCreateModal";

const TabPane = Tabs.TabPane;

class CreateModal extends PureComponent {

    handleCreate = () => {
        const principal = getPrincipal();
        const {dispatch, model, feeList, homeFee, sheetFee, currency, sheetCurrency, vehiclePrices, linePrices} = this.props;
        model.contract.signedDate = formatDate(model.contract.signedDate);
        model.contract.expiresDate = formatDate(model.contract.expiresDate);
        model.contract.remindDate = formatDate(model.contract.remindDate);
        const fees = [];
        feeList.forEach(item => {
            if (item.feeCount && item.feeCount > 0) {
                fees.push(item);
            }
        });
        model.homeFee = homeFee;
        model.sheetFee = sheetFee;
        model.currency = currency;
        model.sheetCurrency = sheetCurrency;
        model.vehiclePrices = vehiclePrices;
        let organizationId;
        if (model.organizationId === undefined || model.organizationId === null || model.organizationId.length === 0) {
            organizationId = principal.organizationId;
        } else {
            organizationId = model.organizationId;
        }
        dispatch(create({
            ...model,
            "organizationId": organizationId,
            "opId": principal.id,
            fees: fees,
            "linePriceImportDtoList": linePrices
        })).then(action => {
            if (action.error !== true) {
                dispatch(hideCreate());
                dispatch(query({"userId": getPrincipal().id, "currentOrgId": getCurrentOrganization()}));
            }
        });
    };
    handleCancel = () => {
        const {dispatch} = this.props;
        dispatch(hideCreate());
    };

    render() {
        const {loading, visible} = this.props;
        return (
            <Modal
                title="新增"
                visible={visible}
                onOk={this.handleCreate}
                onCancel={this.handleCancel}
                confirmLoading={loading}
                okText="保存"
                cancelText="取消"
                width="90%"
            >
                <Tabs defaultActiveKey="1">
                    <TabPane tab="客户信息" key="1">
                        <ClientModal/>
                    </TabPane>
                    <TabPane tab="合同信息" key="2">
                        <ContractModal/>
                    </TabPane>
                    <TabPane tab="线路价格" key="3">
                        <LinePriceInCreateModal/>
                    </TabPane>
                    <TabPane tab="费用信息" key="4">
                        <ClientFeeModal/>
                    </TabPane>
                    <TabPane tab="车型费用" key="5">
                        <VehicleFeeModal/>
                    </TabPane>
                </Tabs>
            </Modal>

        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.client.create,
        feeList: state.client.clientFee.feeList,
        homeFee: state.client.clientFee.homeFee,
        sheetFee: state.client.clientFee.sheetFee,
        currency: state.client.clientFee.currency,
        vehiclePrices: state.client.clientVehicleFee.vehiclePrices,
        linePrices: state.client.importLinePriceInCreateModal.dataSource
    };
};

export default connect(mapStateToProps)(CreateModal);
